package com.loren.oauth.service;

import com.loren.oauth.data.dto.SecurityUserInfo;
import com.loren.oauth.data.dto.UserAuthInfo;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.Map;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

	private final static Map<String, UserAuthInfo> USER_AUTH_INFO_MAP = new LinkedHashMap<>();

	static {
		USER_AUTH_INFO_MAP.put("13000000000", UserAuthInfo.builder().userId("123456")
				.userName("东方不败").mobile("13000000000").email("130000@163.com")
				.password("$2a$10$UlxoENCtc2QdtyMyo3a4h.qtTIShU0ostc7GF/ELna5bkwaZw6TPa").build());
		USER_AUTH_INFO_MAP.put("15000000000", UserAuthInfo.builder().userId("123456")
				.userName("独孤求败").mobile("15000000000").email("150000@163.com")
				.password("$2a$10$lDkg6NCOrxmXvHRR91UFI.wmWtiFWaLJYFqWhNQ.dfcIudCl/owsC").build());
	}

    @Override
    public UserDetails loadUserByUsername(String username) {
        if (!USER_AUTH_INFO_MAP.containsKey(username)) {
            throw new UsernameNotFoundException("username not found");
        }
        UserAuthInfo userAuthInfo = USER_AUTH_INFO_MAP.get(username);
        return new SecurityUserInfo(username, userAuthInfo.getPassword(), userAuthInfo.isEnabled(), true, true,
                userAuthInfo.isUnlock(), AuthorityUtils.commaSeparatedStringToAuthorityList("admin"), userAuthInfo);
    }
}
